package com.netcracker.studportal.server.model;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

@Entity
@Table(name = "Students")
public class Student implements Serializable {
	
	@Id
	@Column(name= "student_id")
	private Long user_id;
	
	@Column(name= "first_name")
	private String stud_first_name;
	
	@Column(name= "last_name")
	private String stud_last_name;
	
	@Column(name= "birthday")
	private Date birthday;
	
	@Column(name= "course")
	private Integer course;
	
	@ManyToOne
	@JoinColumn(name = "faculty_id")
	private Faculty facult;
	
	@Column(name= "group_num")
	private Integer group_num;
	
	@OneToMany(fetch=FetchType.EAGER)
	@JoinColumn(name = "student_id")
	private List<ExamWithResult> exam;
	
	@Column(name= "note")
	private String note;
	
	//getters
	
	public Long getId(){return this.user_id;}
	
	public String getFirst_name(){return this.stud_first_name;}
	
	public String getLast_name(){return this.stud_last_name;}
	
	public Date getBirthday(){return this.birthday;}
	
	public Integer getCourse(){return this.course;}
	
	public Faculty getFaculty(){return this.facult;}
	
	public Integer getGroup_num(){return this.group_num;}
	
	public List<ExamWithResult> getExamWithResult(){return this.exam;}
	
	public String getNote(){return this.note;}
	
	//setters
	
	public void setId(Long id) {this.user_id = id;}
	
	public void setFirst_name(String first_name) {this.stud_first_name = first_name;}
	
	public void setLast_name(String last_name) {this.stud_last_name = last_name;}
	
	public void setBirthday(Date birthday){this.birthday=birthday;}
	
	public void setCourse(Integer course){this.course=course;}
	
	public void setFaculty(Faculty facult){this.facult=facult;}
	
	public void setGroup_num(Integer group_num){this.group_num=group_num;}
	
	public void setExamWithResult(List<ExamWithResult> exam){this.exam=exam;}
	
	public void setNote(String note){this.note=note;}
	
	public JSONObject kJSON(){
		JSONObject obj=new JSONObject();
		if(this.user_id!=null)obj.put("student_id", this.getId().toString());
		obj.put("stud_first_name", this.stud_first_name);
		obj.put("stud_last_name", this.stud_last_name);
		if(this.birthday!=null) {
			SimpleDateFormat format=new SimpleDateFormat("dd.MM.yyyy");		
			obj.put("birthday", format.format(this.birthday));
		}
		if(this.course!=null) obj.put("course", this.course.toString());
		if(this.facult!=null){
			if(this.facult.getId()!=null)obj.put("faculty_id", this.facult.getId().toString());
			obj.put("faculty_name", this.facult.getName());
		}
		if(this.group_num!=null)obj.put("group_num", this.group_num.toString());
		
		if(this.exam!=null){
			JSONArray list = new JSONArray();
			for(ExamWithResult ex:this.getExamWithResult()) 
			{list.add(ex.kJSON());}
			
			obj.put("ExamWithResult", list);
		}
		
		obj.put("note", this.note);
		
		return obj;	
	}	
	
}
